<template>
  <div class="login-content">
    <!--   class="login-header" -->
    <van-nav-bar title="修改密码" left-arrow @click-left="onClickLeft" />
    <van-row>
      <van-col span="24">
        <van-form @submit="onSubmit">
          <van-field
            v-model="oldPwd"
            label="原密码"
            name="oldPwd"
            type="password"
            placeholder="请输入原密码"
            :rules="[{ required: true, message: '请输入原密码' }]"
          />
          <van-field
            v-model="newPwd"
            label="新密码"
            type="password"
            name="newPwd"
            placeholder="请输入新密码"
            :rules="[{ required: true, message: '请输入新密码' }]"
          />
          <van-field
            v-model="confirmPwd"
            label="确认密码"
            type="password"
            name="confirmPwd"
            placeholder="请再次输入新密码"
            :rules="[{ required: true, message: '请再次输入新密码' }]"
          />
          <div style="margin: 16px;">
            <van-button round block type="info" native-type="submit">确认</van-button>
          </div>
        </van-form>
      </van-col>
    </van-row>
  </div>
</template>
<script>
import { Toast } from "vant";
import { updatePwd } from "@/api/personal/personal";
import eventBus from "@/api/public/eventBus";
export default {
  data() {
    return {
      show: "false",
      value: "",
      message: "",
      confirmPwd: "",
      oldPwd: "",
      newPwd: "",
      msg: "",
      flag: false
    };
  },
  methods: {
    // 验证
    validate(values) {
      if (this.newPwd === this.confirmPwd) {
        //  this.msg="两次密码相同";
        this.flag = true;
        let userData = this.$session.get("user"); //获取session

        let condition = {
          userId: JSON.parse(userData).id,
          cPassWord: values.oldPwd,
          passWord: values.newPwd
        };
        updatePwd(condition, data => {
          data.success === true ? Toast(data.info) : Toast(data.info);
        });
      } else {
        Toast("两次输入的密码不相同!");
        // flag = false;
      }
    },

    // 提交
    onSubmit(values) {
      this.validate(values);
    },
    // 返回事件
    onClickLeft() {
      //   Toast("返回");
      //   this.$router.go(-1);
      this.$router.go(-1);
      //   this.$router.back(-1);
    }
  },
  beforeDestroy() {
    eventBus.$emit("retain", true); //传递一个map，id是key，value是value
  }
};
</script>
<style lang="scss" scoped>
.login-content {
  .login-header {
    height: 60px;
    width: 100%;
    color: white;
    line-height: 60px;
    background-color: #1989fa;
    .van-nav-bar .van-icon {
      color: white;
    }
  }
}
</style>